package util.webSearch;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedList;

import util.nlp.HierarchyHash;
import util.string.StringAnalysis;

/**
 * This class contains the data structures holding the delicious tags
 * for a set of web results and the logic to create semantic clusters on these tags
 * 
 *  
 * @author Sergio
 * 
 */
public class TagAggregator {

	/**
	 * @param args
	 */

	public Hashtable<String, Long> tags = new Hashtable<String, Long>();
	
	public HashSet<String> set = new HashSet<String>();

	
	
	
	public Hashtable<String, Long> getTags() {
		return tags;
	}
	
	



	public void fetchTags(String query, int number_of_results, int min_freq){
		
		
		
		tags.clear();
		set.clear();
		
		 LinkedList<WebResult> results = GoogleQuery.googleQuery(query,number_of_results);
		
		updateTags(results);
		
		filterByMinFreq(min_freq);
	}
	
	
	



	private void filterByMinFreq(int minFreq) {
		// TODO Auto-generated method stub
		
		
		Enumeration<String> keys = tags.keys();
		
		while(keys.hasMoreElements()){
			
			String key = keys.nextElement();
			if(tags.get(key)<minFreq){
			tags.remove(key);
			set.remove(key);
			}
				
		}
		
	}





	private void updateTags(LinkedList<WebResult> list) {

		if (list != null) {

			for (int i = 0; i < list.size(); i++) {

				updateTags(list.get(i));

			}

		}

	}
	
	private void updateTags(WebResult result) {

		DeliciousURLInfo del = result.getDelicious();

		if (del != null) {

			Hashtable<String, Long> hash = del.getFrequent_tags();

			if (hash != null) {

				Enumeration<String> keys = hash.keys();

				while (keys.hasMoreElements()) {

					String key = keys.nextElement();
					
					if (tags.containsKey(key)) {

						Long tmp = tags.get(key);

						tags.put(key, tmp + hash.get(key));

					} else {

						tags.put(key, hash.get(key));
						set.add(key);
					}

				}

			}

		}

	}
	
	
	
	public void addStringTags(String t){
		
		
		String tt[] = t.split("\\s+");
		
		for(int i =0 ; i < tt.length; i++){
			
			if(!StringAnalysis.isStopWord(tt[i]))
			set.add(tt[i]);
		}
		
	}
	
	public String toString(){
		
		
		Enumeration<String> keys = tags.keys();
		
		String t="";
		while(keys.hasMoreElements()){
			
			
			String key = keys.nextElement();
			
			t= t+ key + "\t"  + tags.get(key) + "\n";
			
		}
		
		return t;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		TagAggregator aggregator = new TagAggregator();
		String path = "";

		path = "/home/sergio/Documents/delicious_stats/tag_hierarchy.txt";

		
		aggregator.fetchTags("cars", 10,5);
		
		System.out.println(aggregator.toString());
		
		//HierarchyHash hierarchy = new HierarchyHash(path);
		//hierarchy.generateClusters(aggregator.tags.keys());
		//hierarchy.printHierarchy();
		
	}

}
